package faf.web;

import java.io.PrintWriter;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import faf.business.AnswerManager;
import faf.entities.Answer;

public class AttachementController implements Controller {
	AnswerManager manager;

	public void setManager(AnswerManager manager) {
		this.manager = manager;
	}

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Answer answer = manager.get(Long.parseLong(request.getParameter("answer_id")));

		response.setContentType("application/octet-stream" );  
	 	response.setHeader("Content-disposition", "attachment; filename=\""+ answer.getValue() +"\"" );
	 	ServletOutputStream out = response.getOutputStream();
	 	out.flush();
	 	byte[] file = answer.getFile();
	 	out.write(file, 0, file.length);
		return null;
	}
}
